package com.wlgc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wlgc.dao.UserDao;
import com.wlgc.entity.RestBean;
import com.wlgc.entity.Student;
import com.wlgc.entity.User;
import com.wlgc.service.RegisterService;
import com.wlgc.service.StudentService;
import com.wlgc.service.UserService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
@Service
public class RegisterServiceImpl implements RegisterService {
    @Resource
    StringRedisTemplate template;
@Resource
    StudentService studentService;
@Resource
    UserService userService;
    @Override
    public RestBean register(int id, String username, String password,String email) {
        Student student =studentService.getStudentBySid(id);
        if (student==null)
        {
            return new RestBean(500,"您输入的学号不存在!");
        }
        if(!student.getEmail().equals(email))
            return new RestBean<>(500,"注册失败邮箱与学号不匹配！");
        //如果账户已注册，直接返回已经注册

        User user1 = userService.getUserByUsername(username);
        if (!ObjectUtils.isEmpty(user1))
        return new RestBean<>(500,"该账号名已存在");
        //注册账号密码
        BCryptPasswordEncoder encoder=new BCryptPasswordEncoder();
     if (userService.addUser(username,encoder.encode(password),id)) {
         template.delete(email + ":VerifyCode:");
         return new RestBean(200, "注册成功");
     }
     return new RestBean<>(500,"注册失败");
    }

}
